//
//  GoodsAttributeView.m
//  RainbowFM
//
//  Created by Kratos on 14/12/17.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "GoodsAttributeView.h"
#import "GoodsAttributeItem.h"

#define GAP_TOP 10.f
#define GAP_W 5.f
#define GAP_H 5.f
#define GAP_Buttom 10.f

#define Label_H 15.f
#define Label_W (CGRectGetWidth(self.bounds) - GAP_W * 3)/2.f

@interface GoodsAttributeView()
@property (nonatomic , strong) NSMutableArray *attributeLabels;

@end

@implementation GoodsAttributeView

-(instancetype)initWithFrame:(CGRect)frame
{
    frame.size.height += (GAP_TOP + GAP_Buttom);
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

-(void)updateAttributes:(NSArray *)attributes
{
    _attributeList = attributes;
    
    if (!_attributeLabels) {
        _attributeLabels = [NSMutableArray array];
    }
    
    for (UILabel *label in _attributeLabels) {
        [label removeFromSuperview];
    }
    [_attributeLabels removeAllObjects];
    
    NSInteger section = 1;
    NSInteger row = 1;
    for (NSDictionary *dic in _attributeList) {
        
        UILabel *attributeLabel = [[UILabel alloc]initWithFrame:CGRectMake(row * GAP_W + (row - 1) * Label_W, GAP_TOP + (section - 1) * (Label_H + GAP_H), Label_W, Label_H)];
        [attributeLabel setBackgroundColor:[UIColor clearColor]];
        [attributeLabel setTextAlignment:NSTextAlignmentLeft];
        [attributeLabel setFont:[UIFont systemFontOfSize:14]];
        [attributeLabel setText:_F(@"%@：%@",dic[PRO_DIC_KEY],dic[PRO_DIC_VALUE])];
        [attributeLabel setAdjustsFontSizeToFitWidth:YES];
        
        [self addSubview:attributeLabel];
        [_attributeLabels addObject:attributeLabel];
        
        
        if (row % 2 == 0) {
            row = 1;
            section++;
        }else
        {
            row++;
        }
    }
    
    CGRect frame = self.frame;
    frame.size.height = _attributeList && _attributeList.count > 0 ? ceil(_attributeList.count/2.f)*Label_H + (ceil(_attributeList.count/2.f)-1) * GAP_H + (GAP_TOP + GAP_Buttom) : (GAP_TOP + GAP_Buttom);
    self.frame = frame;
}

+(CGFloat)getHeight:(NSArray *)attributes
{
    
    CGFloat height = attributes && attributes.count > 0 ? ceil(attributes.count/2.f)*Label_H + (ceil(attributes.count/2.f)-1) * GAP_H + (GAP_TOP + GAP_Buttom) : (GAP_TOP + GAP_Buttom);
    return height;
}

@end
